<template>
  <div class="app-container">
    <el-form class="base-search-form" :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="请求方式" prop="reqMethod">
        <el-select v-model="queryParams.reqMethod" placeholder="请选择请求方式" clearable>
          <el-option
            v-for="dict in req_method"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!--<el-form-item label="请求签名开关,on-开启off-关闭" prop="signSwitch">
        <el-select v-model="queryParams.signSwitch" placeholder="请选择请求签名开关,on-开启off-关闭" clearable>
          <el-option
            v-for="dict in sign_verify_switch"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>-->
      <!--<el-form-item label="响应验签开关,on-开启off-关闭" prop="verifySwitch">
        <el-select v-model="queryParams.verifySwitch" placeholder="请选择响应验签开关,on-开启off-关闭" clearable>
          <el-option
            v-for="dict in sign_verify_switch"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>-->
      <!--<el-form-item label="contentType" prop="contentType">
        <el-select v-model="queryParams.contentType" placeholder="请选择" clearable>
          <el-option
            v-for="dict in content_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>-->
      <el-form-item label="渠道号" prop="channelCode">
        <el-input
          v-model="queryParams.channelCode"
          placeholder="请输入渠道号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否启用" prop="enabled">
        <el-select v-model="queryParams.enabled" placeholder="请选择" clearable>
          <el-option
            v-for="dict in sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="base-container mb-50px">
      <el-row :gutter="10" class="mb-10px">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['bus:platUrlBaseConfig:add']"
          >新增</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table border v-loading="loading" :data="platUrlBaseConfigList" @selection-change="handleSelectionChange">
        <!-- <el-table-column type="selection" width="55" align="center" /> -->
        <el-table-column label="请求地址" align="center" prop="url" show-overflow-tooltip />
        <el-table-column label="请求方式" align="center" prop="reqMethod">
          <template #default="scope">
            <dict-tag :options="req_method" :value="scope.row.reqMethod"/>
          </template>
        </el-table-column>
        <el-table-column label="请求签名开关" align="center" prop="signSwitch">
          <template #default="scope">
            <dict-tag :options="sign_verify_switch" :value="scope.row.signSwitch"/>
          </template>
        </el-table-column>
        <el-table-column label="响应验签开关" align="center" prop="verifySwitch">
          <template #default="scope">
            <dict-tag :options="sign_verify_switch" :value="scope.row.verifySwitch"/>
          </template>
        </el-table-column>
        <el-table-column label="contentType" align="center" prop="contentType" show-overflow-tooltip />
        <el-table-column label="渠道号" align="center" prop="channelCode" />
        <el-table-column label="是否启用" align="center" prop="enabled">
          <template #default="scope">
            <dict-tag :options="sys_normal_disable" :value="scope.row.enabled"/>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['bus:platUrlBaseConfig:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['bus:platUrlBaseConfig:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </div>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改接口地址基础信息对话框 -->
    <Dialog v-model="showDialog" :title="dialogTitle">
      <edit-config 
        ref="editConfig"
        @cancel="cancel"
        @confirm="confirm"
      />
    </Dialog>
  </div>
</template>

<script setup name="PlatUrlBaseConfig">
import { listPlatUrlBaseConfig, delPlatUrlBaseConfig } from "@/api/bus/platUrlBaseConfig"
import { nextTick } from "vue"
import EditConfig from './edit.vue'

const editConfig = ref()
const { proxy } = getCurrentInstance()
const { content_type, req_method, sys_normal_disable, sign_verify_switch } = proxy.useDict('content_type', 'req_method', 'sys_normal_disable', 'sign_verify_switch')

const platUrlBaseConfigList = ref([])
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const showDialog = ref(false)
const dialogTitle = ref('')

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    url: null,
    reqMethod: null,
    signSwitch: null,
    verifySwitch: null,
    contentType: null,
    channelCode: null,
    enabled: null
  }
})

const { queryParams } = toRefs(data)

/** 查询接口地址基础信息列表 */
function getList() {
  loading.value = true
  listPlatUrlBaseConfig(queryParams.value).then(response => {
    platUrlBaseConfigList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  showDialog.value = false
  dialogTitle.value = ''
}

const confirm = () => {
  cancel()
  getList()
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  showDialog.value = true
  dialogTitle.value = '添加接口地址基础信息'
}

/** 修改按钮操作 */
function handleUpdate(row) {
  const _id = row.id || ids.value
  showDialog.value = true
  dialogTitle.value = '修改接口地址基础信息'
  nextTick(() => {
    editConfig.value.pageInit(_id)
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除接口地址基础信息编号为"' + _ids + '"的数据项？').then(function() {
    return delPlatUrlBaseConfig(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('bus/platUrlBaseConfig/export', {
    ...queryParams.value
  }, `platUrlBaseConfig_${new Date().getTime()}.xlsx`)
}

getList()
</script>
